/*
 * @Author: xuetao 3168581151@qq.com
 * @Date: 2025-02-26
 * @LastEditors: xuetao 3168581151@qq.com
 * @LastEditTime: 2025-03-13
 * @Description: 
 */

import 'package:chat_client/enums/user_enum.dart';
import 'package:chat_client/providers/individual_provider.dart'
    show IndividualProvider;
import 'package:chat_client/utils/date_formater.dart';
import 'package:chat_client/widgets/c_input.dart';
import 'package:chat_client/widgets/c_list_tile.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

// 输入框修改信息
class UpdateByInput extends StatefulWidget {
  final String oldInfo;
  final String type;
  final String title;
  final String describe;

  const UpdateByInput(
      {super.key,
      required this.oldInfo,
      required this.type,
      required this.title,
      required this.describe});

  @override
  UpdateByInputState createState() => UpdateByInputState();
}

class UpdateByInputState extends State<UpdateByInput> {
  final TextEditingController _newInfoController = TextEditingController();

  bool _isEnabled = true;

  @override
  void initState() {
    super.initState();
    setState(() {
      _newInfoController.text = widget.oldInfo;
    });
  }

  // 按钮是否禁用
  _isButtonEnabled(newInfo) {
    setState(() {
      _isEnabled = (newInfo == widget.oldInfo);
    });
  }

  @override
  void dispose() {
    _newInfoController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    // 获取全局 IndividualProvider 实例
    final model = Provider.of<IndividualProvider>(context);
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
        backgroundColor: Colors.white,
        toolbarHeight: 70,
        actions: [
          ElevatedButton(
            onPressed: _isEnabled
                ? null
                : () => model.updateUser<String>(
                    type: widget.type,
                    oldInfo: widget.oldInfo,
                    newInfo: _newInfoController.text.trim()),
            style: ButtonStyle(
              elevation: WidgetStateProperty.all(2.0),
            ),
            child: Text('保存'),
          )
        ],
      ),
      backgroundColor: Colors.white,
      body: Padding(
        padding: EdgeInsets.only(left: 20, right: 20),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            SizedBox(height: 20),
            InputField(
              controller: _newInfoController,
              icon: Icons.edit,
              simplified: true,
              onChanged: (text) => _isButtonEnabled(text),
            ),
            SizedBox(height: 20),
            Text(
              widget.describe,
              textAlign: TextAlign.start,
              style: TextStyle(color: Colors.grey, fontSize: 16),
            ),
          ],
        ),
      ),
    );
  }
}

// 选项修改信息
class UpdateByOption extends StatefulWidget {
  final int oldInfo;
  final String type;
  final String title;
  final int widgetIndex;

  const UpdateByOption({
    super.key,
    required this.oldInfo,
    required this.type,
    required this.title,
    required this.widgetIndex,
  });

  @override
  UpdateByOptionState createState() => UpdateByOptionState();
}

class UpdateByOptionState extends State<UpdateByOption> {
  late int _isSelectedValue;

  bool _isEnabled = true;

  @override
  void initState() {
    super.initState();
    setState(() {
      _isSelectedValue = widget.oldInfo;
    });
  }

  // 按钮是否禁用
  _updateSelection(newInfo) {
    if (newInfo == _isSelectedValue) return;
    _isSelectedValue = newInfo;
    setState(() {
      _isEnabled = (newInfo == widget.oldInfo);
    });
  }

  @override
  Widget build(BuildContext context) {
    // 获取全局 IndividualProvider 实例
    final model = Provider.of<IndividualProvider>(context);

    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
        toolbarHeight: 70,
        backgroundColor: Colors.white,
        actions: [
          ElevatedButton(
            onPressed: _isEnabled
                ? null
                : () => model.updateUser<int>(
                    type: widget.type,
                    oldInfo: widget.oldInfo,
                    newInfo: _isSelectedValue),
            style: ButtonStyle(
              elevation: WidgetStateProperty.all(2.0),
            ),
            child: Text('保存'),
          )
        ],
      ),
      body: widget.widgetIndex == 1
          ? _buildSelectGender(model)
          : _buildSelectAddType(model),
    );
  }

  _buildTralling(index) {
    return index == _isSelectedValue ? Icon(Icons.check) : null;
  }

  // 修改验证方式界面
  Widget _buildSelectAddType(IndividualProvider model) {
    return ListView(
      children: [
        CListTile(
            icon: Icons.hail,
            iconColor: Color.fromARGB(255, 67, 236, 126),
            title: model.getAddTypeLabel(UserAddType.public.value),
            trailing: _buildTralling(UserAddType.public.value),
            onTap: () => _updateSelection(UserAddType.public.value)),
        Divider(height: 1),
        CListTile(
            icon: Icons.forward_to_inbox,
            iconColor: Color.fromARGB(255, 67, 180, 236),
            title: model.getAddTypeLabel(UserAddType.vertify.value),
            trailing: _buildTralling(UserAddType.vertify.value),
            onTap: () => _updateSelection(UserAddType.vertify.value)),
        Divider(height: 1),
        CListTile(
            icon: Icons.mail_lock,
            iconColor: Color.fromARGB(255, 236, 233, 67),
            title: model.getAddTypeLabel(UserAddType.private.value),
            trailing: _buildTralling(UserAddType.private.value),
            onTap: () => _updateSelection(UserAddType.private.value)),
      ],
    );
  }

  // 修改性别界面
  Widget _buildSelectGender(IndividualProvider model) {
    return ListView(
      children: [
        CListTile(
            icon: Icons.help,
            iconColor: Color.fromARGB(255, 53, 54, 54),
            title: model.getGenderLabel(GenderEnum.unKnow.value),
            trailing: _buildTralling(GenderEnum.unKnow.value),
            onTap: () => _updateSelection(GenderEnum.unKnow.value)),
        Divider(height: 1),
        CListTile(
            icon: Icons.female,
            iconColor: Color.fromARGB(255, 62, 158, 218),
            title: model.getGenderLabel(GenderEnum.male.value),
            trailing: _buildTralling(GenderEnum.male.value),
            onTap: () => _updateSelection(GenderEnum.male.value)),
        Divider(height: 1),
        CListTile(
            icon: Icons.male,
            iconColor: Color.fromARGB(255, 236, 151, 67),
            title: model.getGenderLabel(GenderEnum.female.value),
            trailing: _buildTralling(GenderEnum.female.value),
            onTap: () => _updateSelection(GenderEnum.female.value)),
      ],
    );
  }
}

// 列表选项修改信息
class UpdateBySelect extends StatefulWidget {
  final String oldInfo;
  final String type;
  final String title;
  final String describe;

  const UpdateBySelect(
      {super.key,
      required this.oldInfo,
      required this.type,
      required this.title,
      required this.describe});

  @override
  UpdateBySelectState createState() => UpdateBySelectState();
}

class UpdateBySelectState extends State<UpdateBySelect> {
  final TextEditingController _newInfoController = TextEditingController();

  bool _isEnabled = true;

  @override
  void initState() {
    super.initState();
    setState(() {
      _newInfoController.text = widget.oldInfo;
    });
  }

  // 按钮是否禁用
  _isButtonEnabled(newInfo) {
    setState(() {
      _isEnabled = (newInfo == widget.oldInfo);
    });
  }

  @override
  void dispose() {
    _newInfoController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    // 获取全局 IndividualProvider 实例
    final model = Provider.of<IndividualProvider>(context, listen: false);
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
        backgroundColor: Colors.white,
        toolbarHeight: 70,
        actions: [
          ElevatedButton(
            onPressed: _isEnabled
                ? null
                : () => model.updateUser<String>(
                    type: widget.type,
                    oldInfo: widget.oldInfo,
                    newInfo: _newInfoController.text.trim()),
            style: ButtonStyle(
              elevation: WidgetStateProperty.all(2.0),
            ),
            child: Text('保存'),
          )
        ],
      ),
      backgroundColor: Colors.white,
      body: Padding(
        padding: EdgeInsets.only(left: 20, right: 20),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            SizedBox(height: 20),
            InputField(
              controller: _newInfoController,
              icon: Icons.edit,
              simplified: true,
              onChanged: (text) => _isButtonEnabled(text),
            ),
            SizedBox(height: 20),
            Text(
              widget.describe,
              textAlign: TextAlign.start,
              style: TextStyle(color: Colors.grey, fontSize: 16),
            ),
          ],
        ),
      ),
    );
  }
}

// 生日选择器
Future<void> birthdayPicke({
  required BuildContext context,
  required IndividualProvider model,
}) async {
  final newBirthday = await datePicker(
      context: context, initTime: model.user.birthday, helpText: '生日选择');
  if (newBirthday == null) return;
  int timestamp = newBirthday.millisecondsSinceEpoch;
  if (timestamp == model.user.birthday) return;
  if (!context.mounted) return;
  model.updateUser<int>(
    type: 'birthday',
    oldInfo: model.user.birthday,
    newInfo: timestamp,
    goback: false,
  );
}
